package com.wangsd.member.controller;

import com.gitee.fastmybatis.core.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.controller.AjaxResult;
import com.wangsd.common.controller.BaseController;
import com.wangsd.common.jwt.LoginUser;
import com.wangsd.common.jwt.TokenService;
import com.wangsd.common.utils.ServletUtils;
import com.wangsd.member.entity.GoldRecord;
import com.wangsd.member.service.GoldRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 金币流水记录Controller
 *
 * @author wsd
 * @date 2021-06-28
 */
@Api(tags = "金币流水记录管理")
@RestController
@RequestMapping("/member/goldRecord")
public class GoldRecordController extends BaseController {
    @Autowired
    private GoldRecordService goldRecordService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "查询金币流水记录列表")
    @GetMapping("/list")
    public AjaxResult<PageInfo<GoldRecord>> list(GoldRecord goldRecord) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        Page page = PageHelper.startPage(ServletUtils.getParameterToInt("pageNum"), ServletUtils.getParameterToInt("pageSize"));
        List<GoldRecord> list = goldRecordService.selectGoldRecordList(goldRecord);
        return AjaxResult.success(getPageInfo(page, list));
    }


    @ApiOperation(value = "获取金币流水记录详细信息")
    @GetMapping("/{id}")
    public AjaxResult<GoldRecord> getInfo(@PathVariable("id") Long id) {
        GoldRecord goldRecord = goldRecordService.selectGoldRecordById(id);
        return AjaxResult.success(goldRecord);
    }

    @ApiOperation(value = "新增/修改金币流水记录")
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody GoldRecord goldRecord) {
        if (goldRecord.getId() == null){
            goldRecordService.insertGoldRecord(goldRecord);
        } else {
            goldRecordService.updateGoldRecord(goldRecord);
        }
        return AjaxResult.success("success");
    }


    @ApiOperation(value = "删除金币流水记录")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        goldRecordService.deleteGoldRecordById(id);
        return AjaxResult.success("success");
    }
}
